import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//1.引入element-plus组件库
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

import mitt from 'mitt';
//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";

const app = createApp(App)
//4.遍历ElementPlusIconsVue对象中的所有图标组件并注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.app.use(ElementPlus) 使用Element-plus组件库
//5.2 app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(ElementPlus,{ locale: zhCn }).use(store).use(router).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

//配置全局方法，获取当前登录用户信息
window.getUser = () => {
    return localStorage.getItem('user')? JSON.parse(localStorage.getItem('user')) : null;
}

//创建全局方法，用来获取指定dictType对应的字典项
window.loadDictOptions = (object,dictType)=>{
    //向后端发请求获取字典项
    axios.get(getBaseURL(4)+'/v1/medical/medicalSystem/selectDictDetail',{
        params: {
            dictType: dictType
        }
    }).then((response)=>{
        if(response.data.code == 2000){
            //将字典项保存到对象中
            object.value = response.data.data;
        }else{
            ElMessage.error(response.data.msg);
        }
    })
}

window.getBaseURL = (BaseNumber) => {
    if (BaseNumber != null && BaseNumber !== '')
    return 'http://localhost:808' + BaseNumber;
    else
    return 'http://localhost:8080';
}

app.config.globalProperties.getBaseURL = getBaseURL;

window.BASE_URL = getBaseURL();

